﻿using Microsoft.Extensions.DependencyInjection;
using ZeroSum.DependencyInjection.Attributes;
using ZeroSum.Plugin.Authentication.JwtBearer.Authorization;
using ZeroSumZSA.Core.DomainServices.Menus;
using ZeroSumZSA.Core.DomainServices.Users;

namespace ZeroSumZSA.Core.Authorization;

[Register(ServiceLifetime.Scoped)]
public class ZeroSumAuthorization : IZeroSumAuthorization
{
    private readonly IMenuDomainService _menuDomainService;
    private readonly IUserDomainService _userDomainService;

    public ZeroSumAuthorization(IUserDomainService userDomainService, IMenuDomainService menuDomainService)
    {
        _userDomainService = userDomainService;
        _menuDomainService = menuDomainService;
    }

    public async Task<bool> CheckForPermission(long userId, string? permission = "")
    {
        var roleIds = await _userDomainService.GetRoleIdsByUserId(userId);
        return await _menuDomainService.CheckForPermission(roleIds, permission);
    }


    public async Task<bool> IsSuperAdmin(long userId)
    {
        var user = await _userDomainService.GetByIdAsync(userId);
        return user.IsAdmin;
    }
}